﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI;
using DevExpress.Web.ASPxHeadline;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxCallback;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Data;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;

public partial class CCList : ICPRelationshipPage
{
    public CCData ccData = new CCData();
    #region Properties
    public String m_sxMode
    {
        get
        {
            if (!String.IsNullOrEmpty(Request.QueryString["mode"]))
            {
                return Context.ReadQuery("mode").ToUpper();
            }
            else
            {
                return "PLANNED";
            }
        }
    }
    #endregion Properties

    #region Event Handlers

    protected override void Page_Load(object sender, EventArgs e)
    {
        m_lsPermittedRoles.Add(ICPUserRole.CAR);
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Relationship, Courtesy Call - Call List";
        if (!IsPostBack && !IsCallback)
        {
            
        }
        InitCallList(m_sxMode);
    }
    protected void cbkList_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        e.Result = HandleList(e.Parameter);
    }
    protected void dgCallView_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        if(e.Parameters.StartsWith("REFRESH"))
        {
            InitCallList(m_sxMode);
        }
        else if (e.Parameters.StartsWith("CANCEL"))
        {
            HandleCancelation(e.Parameters);
        }
        
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        String sxFilename = String.Format("Courtesy Call List - {0}", DateTime.Now.ToString());
        dgExporter.WriteXlsToResponse(sxFilename);
    }
    #endregion Event Handlers

    #region Custom Methods
    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    public void InitCallList(String sxMode)
    {
        Cached.CourtesyCallTable cachedCC = new Cached.CourtesyCallTable();
        DataTable dtCallList = cachedCC.GetCallList(sxMode);

        dgCallView.DataSource = dtCallList;
        dgCallView.DataBind();
        if (sxMode.Equals("PLANNED"))
        {
            dgCallView.Columns["VIEW"].Visible = false;
        }
        else
        {
            dgCallView.Columns["EDIT"].Visible = false;
            dgCallView.Columns["REALIZE"].Visible = false;
            dgCallView.Columns["CANCEL_CALL"].Visible = false;
        }
    }

    private string HandleList(string sxParam)
    {
        String[] sxSplitted = sxParam.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);

        String sxOutput = String.Empty;
        int iCCID = -1;
        DateTime dtmPlannedDate = DateTime.MinValue;
        
        switch (sxSplitted[0].ToUpper())
        {
            case "EDIT":
                iCCID = Convert.ToInt32(sxSplitted[1]);
                dtmPlannedDate = DBQuery.CourtesyCallTable.GetPlannedDateById(iCCID); 
                sxOutput = CCManager.BuildEditor_EditPlannedDate(iCCID.ToString(),iCCID, dtmPlannedDate);
                break;
            case "SAVE":
                iCCID = Convert.ToInt32(sxSplitted[1]);
                dtmPlannedDate = Convert.ToDateTime(sxSplitted[2]);
                DBQuery.CourtesyCallTable.EditPlannedDate(iCCID,dtmPlannedDate);
                Cached.CourtesyCallTable cachedCC = new Cached.CourtesyCallTable();
                cachedCC.RemoveCache();
                InitCallList(m_sxMode);
                break;
        }

        return sxOutput;
    }
    private void HandleCancelation(string sxParam)
    {
        String[] sxSplitted = sxParam.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
        int iCCID = Convert.ToInt32(sxSplitted[1]);
        String sxReason = sxSplitted[2].ToString();
        DBQuery.CourtesyCallTable.CancelCall(iCCID, sxReason);
        Cached.CourtesyCallTable cachedCC = new Cached.CourtesyCallTable();
        cachedCC.RemoveCache();
        //InitCallList(m_sxMode);
    }
    #endregion Custom Methods
}
